<?php

namespace HelloAdmin\Controller;

class ShareController extends CommonController {

    private $Share;

    public function __construct() {
        parent::__construct();
        $this->Share = D('Common/ShareLog');
    }

    public function index() {
        $post = $this->GetParam('ship_name,ship_mobile,status,pay_status');
        if (!empty($post)) {
            $where = $this->search($post);
        }


        $count = $this->Share->GetCount($where);
        parent::backPage($count);
        $data = $this->Share->GetDataByWhere($where, 'createtime DESC', $this->offset, $this->numPerPage, $fields = '');
        $this->assign('Count', $count);

        $this->assign('data', $data[0]);
        $this->display();
    }

    /**
     * 
     * 新增备注
     */
    public function addRemark() {
        
        $goods_id = $this->GetParam('goods_id');
        $data = $this->Share->GetDataById($goods_id, 'goods_id,goods_name,goods_price,ship_name,mark_text');

        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 修改支付状态
     */
    public function edit() {
        $goods_id = $this->GetParam('goods_id');
        $data = $this->Share->GetDataById($goods_id, 'goods_id,ship_name,pay_status,status,total_amount,member_id,status,mark_text');

        $this->assign('data', $data);
        $this->display();
    }

    public function saveRemark() {
        if ($_POST['goods_id'] > 0) {
            $post = $this->GetParam('mark_text,goods_id');
            $data = array('goods_id' => $post['goods_id'], 'mark_text' => $post['mark_text ']);
            if ($this->Share->SaveData($data)) {
                $this->success('备注修改成功');
                exit;
            }
        }
        $this->error('备注修改失败');
    }

    private function search($post) {
        //var_dump($post);
        $where = ' AND 1=1 ';
        if (!empty($post['ship_mobile'])) {
            $where .= "AND `ship_mobile` like '" . $post['ship_mobile'] . "%' ";
        }
        if (!empty($post['ship_name'])) {
            $where .= "AND `ship_name` like '" . $post['ship_name'] . "%' ";
        }
        if (!empty($post['goods_id'])) {
            $where .= "AND `goods_id` like '" . $post['goods_id'] . "%' ";
        }
        if (!empty($post['pay_status'][0])) {
            $where .= "AND `pay_status` = '" . $post['pay_status'][0] . "' ";
            $this->assign('sex1', $post['pay_status'][0]);
        }
        if (!empty($post['pay_status'][1])) {
            $where .= "AND `pay_status` = '" . $post['pay_status'][1] . "' ";
            $this->assign('sex2', $post['pay_status'][1]);
        }
        if (!empty($post['status'])) {
            $where .= "AND `status` = '{" . $post['status'] . "}' ";
        }
        return $where;
    }

}
